﻿using Asp.Versioning;
using FlyingEye.ControllerServices;
using FlyingEye.Exceptions;
using FlyingEye.Monitors.Models;
using FlyingEye.Monitors.Repositories;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace FlyingEye.Controllers
{
    [Authorize]
    [ControllerName("StringValueMonitorGroup")]
    [ApiVersion("1.0")]
    [Route("api/device-monitor-service/v{version:apiVersion}/string-value-monitor-groups")]
    public class StringValueMonitorGroupController : DefaultController
    {
        private readonly IStringValueMonitorGroupRepository _stringValueMonitorGroupRepository;
        public StringValueMonitorGroupController(IStringValueMonitorGroupRepository stringValueMonitorGroupRepository)
        {
            _stringValueMonitorGroupRepository = stringValueMonitorGroupRepository;
        }

        [HttpGet("{id}", Name = "GetByStringValueMonitorGroupId")]
        public async Task<ActionResult<StringValueMonitorGroupResponse>> GetAsync(Guid id)
        {
            return await this.GetInvokeAsync(async () =>
            {
                var model = await _stringValueMonitorGroupRepository.GetAsync(id);

                return ObjectMapper.Map<StringValueMonitorGroupModel, StringValueMonitorGroupResponse>(model);
            });
        }

        [HttpGet]
        public async Task<ActionResult<List<StringValueMonitorGroupResponse>>> GetListAsync()
        {
            return await this.GetInvokeAsync(async () =>
            {
                var list = await _stringValueMonitorGroupRepository.GetListAsync();
                return ObjectMapper.Map<List<StringValueMonitorGroupModel>, List<StringValueMonitorGroupResponse>>(list);
            });
        }

        [HttpPut]
        public async Task<ActionResult<StringValueMonitorGroupResponse>> UpdateAsync([FromBody] UpdateStringValueMonitorGroupRequest request)
        {
            if (request.Id == Guid.Empty)
            {
                throw new AppArgumentValidationException("ID 不能为空");
            }

            if (request.ReadStringValueTaskId == Guid.Empty)
            {
                throw new AppArgumentValidationException("监听任务 ID 不能为空");
            }

            if (string.IsNullOrWhiteSpace(request.GroupName))
            {
                throw new AppArgumentValidationException("监听点客户端名称不能为空");
            }

            return await this.PutInvokeAsync(async () =>
            {
                var model = ObjectMapper.Map<UpdateStringValueMonitorGroupRequest, StringValueMonitorGroupModel>(request);
                var result = await _stringValueMonitorGroupRepository.UpdateAsync(model);
                return ObjectMapper.Map<StringValueMonitorGroupModel, StringValueMonitorGroupResponse>(result);
            });
        }

        [HttpPost]
        public async Task<ActionResult<StringValueMonitorGroupResponse>> InsertAsync([FromBody] CreateStringValueMonitorGroupRequest request)
        {
            if (request.ReadStringValueTaskId == Guid.Empty)
            {
                throw new AppArgumentValidationException("监听任务 ID 不能为空");
            }

            if (string.IsNullOrWhiteSpace(request.GroupName))
            {
                throw new AppArgumentValidationException("监听点客户端名称不能为空");
            }

            return await this.InsertInvokeAsync(async () =>
            {
                var model = ObjectMapper.Map<CreateStringValueMonitorGroupRequest, StringValueMonitorGroupModel>(request);
                var result = await _stringValueMonitorGroupRepository.InsertAsync(model);
                return ObjectMapper.Map<StringValueMonitorGroupModel, StringValueMonitorGroupResponse>(result);
            }, "GetByStringValueMonitorGroupId", x => new { id = x.Id });
        }

        [HttpDelete("{id}")]
        public async Task<NoContentResult> DeleteAsync(Guid id)
        {
            return await this.DeleteInvokeAsync(async () =>
            {
                await _stringValueMonitorGroupRepository.DeleteAsync(id);
            });
        }
    }
}
